package br.com.iswe.gwt.ui.client;


import br.com.iswe.gwt.ui.client.base.BaseConsultarPanel;
import br.com.iswe.gwt.ui.client.comp.ISWEButton;
import br.com.iswe.gwt.util.client.ClientUtils;
import br.com.iswe.gwt.util.client.Listener;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;

public class LookUpPopUp extends ModalPopupPanel {
	
	protected Widget conteudo = null;
	
	protected PnBotoesInferior botoesInferior = new PnBotoesInferior();
	protected ISWEButton fechar = PnBotoesInferior.createButtonFechar();
	
	protected ClickListener listenerFechar = new ClickListener(){
		public void onClick(Widget sender) {
			hide();
		}
	};

	protected String titulo;
	
	protected LookUpPopUp() {
		//construtor chamada apenas pelas classes filhas
		//quanto chamam este construtor apos devem chamar o metodo setup()
	}
	public LookUpPopUp(Widget conteudo, String titulo) {
		this.conteudo = conteudo;
		this.titulo = titulo;
		setup();
	}
	
	protected void setup() {
		//Layout
		Widget barraSuperior = BarraTitulo.buildBarraLookUp(titulo, listenerFechar);
		
		DockPanel deck = new DockPanel();
		//Barra Titulo
		deck.add(barraSuperior, DockPanel.NORTH);
		//
		deck.add(this.conteudo, DockPanel.CENTER);
		fechar.addClickListener(listenerFechar);
		botoesInferior.addButton(fechar);
		
		deck.add(botoesInferior, DockPanel.SOUTH);
		//adiciona no popup
		setWidget(deck);
		//Style
		setStyleName("lookup");
		hide();	
	}

	protected Listener searchListener = null;
	
	protected KeyboardListener keyboardListener = new KeyboardListener(){
		public void onKeyDown(Widget sender, char keyCode, int modifiers) {	
		}
		public void onKeyPress(Widget sender, char keyCode, int modifiers) {	
		}
		public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		}
	};

	protected String labelPropertyName = null;
	public LookUpPopUp(final Widget conteudo, String titulo, Listener searchListener) {
		this(conteudo, titulo);
		this.searchListener = searchListener;
	}
	public LookUpPopUp(final Widget conteudo, String titulo, final String labelPropertyName) {
		this(conteudo, titulo);
		this.labelPropertyName  = labelPropertyName;
		setupSearchListener();
	}

	protected void setupSearchListener() {
		if(conteudo instanceof BaseConsultarPanel && labelPropertyName != null){
			this.searchListener = new Listener(){
				public void execute(Object parametro) {
					BaseConsultarPanel consultarPanel = (BaseConsultarPanel)conteudo;
					//Quando abrir o lookup ja com uma string de pesquisa, pesquisar pelo Label.
					consultarPanel.getFiltroPanel().getForm().setProperty(labelPropertyName, (String)parametro);
					consultarPanel.consultar();
				}
			};
		}
	}
	public void addFecharListener(ClickListener fecharListener) {
		fechar.addClickListener( fecharListener );
	}

	public PnBotoesInferior getBotoesInferior() {
		return botoesInferior;
	}

	public void show() {
		super.show();
		ClientUtils.centerPopUpHorizontal(this);
	}
	
	public void show(String searchString) {
		super.show();
		ClientUtils.centerPopUpHorizontal(this);
		if(searchListener != null && ClientUtils.isNotEmpty(searchString)){
			searchListener.execute(searchString);
		}
	}

	
}
